capture program drop msdgp
program define msdgp
{
  syntax, n(integer) [wiggle(real 1)]
  drop _all
  set obs `n'
  gen X1=(runiform()*2-1)
  gen X2=(runiform()*2-1)-X1
  gen X3=(runiform()*2-1)
  gen X4=(runiform()*2-1)-X3
  gen U=runiform()
  gen u=ln(U/(1-U))
  //index
  gen X1X2=X1*X2
  gen X1X3=X1*X3
  gen X1X4=X1*X4
  gen X2X3=X2*X3
  gen X2X4=X2*X4
  gen X3X4=X3*X4
  gen lin = X1+X2+X3+X4
  gen inter = 0.65+X1X2+X1X3+X1X4+X2X3+X2X4+X3X4
  if `wiggle'<10  gen g = (1-`wiggle')*lin+`wiggle'*inter
  if `wiggle'==10 gen g = lin
  if `wiggle'==20 gen g = inter
  //selection equation
  gen Tstar=g-u
  gen T=(Tstar>0)
  gen pX=exp(g)/(1+exp(g))
  //outcome
  gen e=rnormal()
  if `wiggle'<10  gen m=(`wiggle'-1)*lin+`wiggle'*inter
  if `wiggle'==10 gen m=inter
  if `wiggle'==20 gen m=lin
  //new outcome equation
  gen y1=T+m+e
  
}
end

